package com.jgc.future;

import java.util.concurrent.*;

/**
 * Created by Administrator on 2022/3/22.
 */
public class CompletableFutureAPIDemo2 {
    public static void main(String[] args) throws ExecutionException, InterruptedException, TimeoutException {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1,10,1L, TimeUnit.SECONDS,new LinkedBlockingQueue<>(50), Executors.defaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy());
        Integer inteterJoin = CompletableFuture.supplyAsync(() -> {
            return 1;
        }).thenApply(f -> {
            f += 2;
            int i = 10/0;
            System.out.println("----1");
            return f;
        }).thenApply(f -> {
            f += 3;
            System.out.println("----2");
            return f;
        }).thenApply(f -> {
            f += 4;
            System.out.println("----3");
            return f;
        }).whenComplete((v, e) -> {
            if (e == null) {
                System.out.println("-----result" + v);
            }
        }).exceptionally(e -> {
            e.printStackTrace();
            return null;
        }).join();
        System.out.println(inteterJoin);


        Integer inteterJoin2 = CompletableFuture.supplyAsync(() -> {
            return 1;
        }).handle((f,e)->{
            System.out.println(1);
            int i = 1/0;
            return f + 2;
        }).handle((f,e)->{
            System.out.println(1);
            return f + 2;
        }).handle((f,e)->{
            System.out.println(1);
            return f + 2;
        }).exceptionally(e->{
           e.printStackTrace();
           return null;
        }).join();
        System.out.println(inteterJoin2);


        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        threadPoolExecutor.shutdown();


    }
}
